$import( "SmitearWebGL.Objects.Parser.Collada.DaeEntity" );
$import( "SmitearWebGL.Objects.Parser.Collada.DaeArray" );
$import( "SmitearWebGL.Objects.Parser.Collada.DaeAccessor" );

SmitearWebGL.Objects.Parser.Collada.DaeSource = SmitearWebGL.Objects.Parser.Collada.DaeEntity.extend(
{
	/******** constructor ********/

	init : function( document, node )
	{
		this.accessor = null;
		this.values = null;

		this._super( document, node );
		this._registClass( "SmitearWebGL.Objects.Parser.Collada.DaeSource" );
	}
	,
	
	/******** public method ********/
	
	destroy : function()
	{
		this._super();

		if(this.accessor)
		{
			this.accessor.destroy();
			this.accessor = null;
		}

		if(this.values)
		{
			this.values = null;
		}
	}
	,
	read : function( node )
	{
		this.values = [];

		if( node.localName != SmitearWebGL.Objects.Parser.Collada.Const.DAE_SOURCE_ELEMENT )
			return;

		this._super( node );

		var data = new SmitearWebGL.Objects.Parser.Collada.DaeArray( this.document, node );

		var program_common = this.getNode( node, SmitearWebGL.Objects.Parser.Collada.Const.DAE_TECHNIQUE_COMMON_ELEMENT);
		if( !program_common )
		{
			this.values = data.values;
			return;
		}

		// As a child of <source>, this element must contain exactly one <accessor> element.
		var acc = this.getNode( program_common, SmitearWebGL.Objects.Parser.Collada.Const.DAE_ACCESSOR_ELEMENT );
		
		$THROW( acc, "As a child of <source>, this element must contain exactly one <accessor> element." );

		this.accessor = new SmitearWebGL.Objects.Parser.Collada.DaeAccessor( this.document, acc );

		for( var i = 0; i < data.count; i += this.accessor.stride )
		{
			if( this.accessor.stride > 1 )
			{
				var tmp = [];
				for( var j = 0; j < this.accessor.stride; j++ )
				{
					// FIXME: spec says things depend on accessor params...
					// if( !this.accessor.params[j] || this.accessor.params[j].name == "" ) continue;
					tmp.push( data.values[i + j] );
				}
				this.values.push( tmp );
			}
			else
				this.values.push( data.values[i] );
		}
	}
}
);